package com.springboot.mybatis.base.service.impl;

import com.springboot.mybatis.base.entity.PageInfo;
import com.springboot.mybatis.base.entity.PageableEntity;
import com.springboot.mybatis.base.enums.ExceptionEnum;
import com.springboot.mybatis.base.repository.BaseRepository;
import com.springboot.mybatis.base.service.BaseService;
import com.springboot.mybatis.base.utils.ExceptionHelper;
import com.springboot.mybatis.base.utils.IdHelper;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseServiceImpl<T extends PageableEntity, ID extends Serializable> implements BaseService<T, ID> {
    public BaseRepository repository;

    public BaseServiceImpl() {
    }

    @Override
    public int logicalHide(ID id) {
        try {
            Map<String, Object> record = new HashMap(2);
            record.put("id", id);
            record.put("isHide", 1);
            return this.getRepository().updateByPrimaryKeySelective(record);
        } catch (Exception var3) {
            throw ExceptionHelper.build(ExceptionEnum.NONSUPPORT_FUN, "隐藏数据");
        }
    }

    @Override
    public int logicalShow(ID id) {
        try {
            Map<String, Object> record = new HashMap(2);
            record.put("id", id);
            record.put("isHide", 0);
            return this.getRepository().updateByPrimaryKeySelective(record);
        } catch (Exception var3) {
            throw ExceptionHelper.build(ExceptionEnum.NONSUPPORT_FUN, "显示数据");
        }
    }

    @Override
    public int logicalDelete(ID id) {
        try {
            Map<String, Object> record = new HashMap(2);
            record.put("id", id);
            record.put("isDelete", 1);
            return this.getRepository().updateByPrimaryKeySelective(record);
        } catch (Exception var3) {
            throw ExceptionHelper.build(ExceptionEnum.NONSUPPORT_FUN, "逻辑删除");
        }
    }
    @Override
    public int deleteByPrimaryKey(ID id) {
        return this.getRepository().deleteByPrimaryKey(id);
    }

    @Override
    public int insert(T record) {
        return this.getRepository().insert(record);
    }

    @Override
    public String insertSelective(T record) {
        String id = IdHelper.nextStringValue();

        try {
            Method method = record.getClass().getDeclaredMethod("setId", String.class);
            if (method != null) {
                method.invoke(record, id);
            }
        } catch (Exception var4) {
        }

        return this.getRepository().insertSelective(record) > 0 ? id : "";
    }

    @Override
    public int updateByPrimaryKeySelective(T record) {
        return this.getRepository().updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(T record) {
        return this.getRepository().updateByPrimaryKey(record);
    }

    @Override
    public T selectById(ID id) {
        return (T)this.getRepository().selectById(id);
    }

    @Override
    public T findOne(T record) {
        return (T)this.getRepository().findOne(record);
    }

    @Override
    public Boolean exist(T record) {
        return this.getRepository().findOne(record) != null;
    }

    @Override
    public PageInfo findAllWithPageInfo(T record) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setData(this.getRepository().findAllWithResult(record));
        return pageInfo;
    }

    @Override
    public PageInfo findPageWithPageInfo(T record) {
        List<T> list = this.getRepository().findPageWithResult(record);
        Integer total = this.getRepository().findPageWithCount(record);
        return new PageInfo(record, total, list);
    }

    @Override
    public List<T> findAllWithResult(T record) {
        return this.getRepository().findAllWithResult(record);
    }

    @Override
    public List<T> findPageWithResult(T record) {
        return this.getRepository().findPageWithResult(record);
    }

    @Override
    public Integer findPageWithCount(T record) {
        return this.getRepository().findPageWithCount(record);
    }

    @Override
    public PageInfo findPageWithPageInfoLike(T record) {
        List<T> list = this.getRepository().findPageWithResultLike(record);
        Integer total = this.getRepository().findPageWithCountLike(record);
        return new PageInfo(record, total, list);
    }
}